#include "Camera.h"

using namespace Renderer;



void Renderer::Camera::ZoomToTarget(double radius)
{
	Vector3D to_eye = glm::normalize(_eye - _target);
	double sin = glm::sin(glm::min(_fieldOfViewX, _fieldOfViewY) * 0.5);
	double distance = (radius / sin);
	_eye = _target + (distance * to_eye);
}

void Renderer::Camera::PerspectiveNearPlaneDistance(double val)
{
	_perspectiveNearPlaneDistance = val;
}

double Renderer::Camera::PerspectiveNearPlaneDistance() const
{
	return _perspectiveNearPlaneDistance;
}

void Renderer::Camera::PerspectiveFarPlaneDistance(double val)
{
	_perspectiveFarPlaneDistance = val;
}

double Renderer::Camera::PerspectiveFarPlaneDistance() const
{
	return _perspectiveFarPlaneDistance;
}

void Renderer::Camera::FieldOfViewY(double val)
{
	_fieldOfViewY = val;
}

double Renderer::Camera::FieldOfViewY() const
{
	return _fieldOfViewY;
}

double Renderer::Camera::FieldOfViewX() const
{
	double fvx = 0.0;
	fvx = (2.0*atan(_aspectRatio*tan(_fieldOfViewY*0.5)));
	return fvx;
}

void Renderer::Camera::AspectRatio(double val)
{
	_aspectRatio = val;
}

double Renderer::Camera::AspectRatio() const
{
	return _aspectRatio;
}

double Renderer::Camera::Height(const Ellipsoid* shape)
{
	return shape->ToGeodetic3D(Eye()).getHeight();
}

Core::Vector3D Renderer::Camera::Right() const
{
	return glm::normalize(glm::cross(Forward(), Up()));
}

Core::Vector3D Renderer::Camera::Forward() const
{
	return glm::normalize(Target() - Eye());
}

Core::Vector3F Renderer::Camera::EyeLow() const
{
	return Core::Vector3F(_eye - Core::Vector3D(EyeHigh()));
}

Core::Vector3F Renderer::Camera::EyeHigh() const
{
	return Core::Vector3F(_eye);
}

void Renderer::Camera::Eye(Core::Vector3D val)
{
	_eye = val;
}

Core::Vector3D Renderer::Camera::Eye() const
{
	return _eye;
}

void Renderer::Camera::Target(Core::Vector3D val)
{
	_target = val;
}

Core::Vector3D Renderer::Camera::Target() const
{
	return _target;
}

void Renderer::Camera::Up(Core::Vector3D val)
{
	_up = val;
}

Core::Vector3D Renderer::Camera::Up() const
{
	return _up;
}

Renderer::Camera::Camera()
{
	_eye = Vector3D(-0.0, -1.0, -0.0);
	_target = Vector3D(0.0, 0.0, 0.0);
	_up = Vector3D(0.0, 0.0, 1.0);

	_fieldOfViewY = Trig::PI / 6.0;
	//_fieldOfViewY = 0.52359877559829882;
	_aspectRatio = 1.0;

	_perspectiveNearPlaneDistance = 0.01;
	_perspectiveFarPlaneDistance = 64.0;
}
